/**
 *Project: jh-framework
 *File: cn.xkshow.util.HtmlHelper.java 
 *Author: admin@xkshow.cn with the copyrite
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-1-27        xkshow.cn         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package cn.xkshow.util;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

public class HtmlHelper {

	public static String filter(String value) {
		if (value == null) {
			return null;
		}
		StringBuffer result = new StringBuffer(value.length());
		for (int i = 0; i < value.length(); ++i) {
			switch (value.charAt(i)) {
			case '<':
				result.append("&lt;");
				break;
			case '>':
				result.append("&gt;");
				break;
			case '"':
				result.append("&quot;");
				break;
			case '\'':
				result.append("&#39;");
				break;
			case '%':
				result.append("&#37;");
				break;
			case ';':
				result.append("&#59;");
				break;
			case '(':
				result.append("&#40;");
				break;
			case ')':
				result.append("&#41;");
				break;
			case '&':
				result.append("&amp;");
				break;
			case '+':
				result.append("&#43;");
				break;
			default:
				result.append(value.charAt(i));
				break;
			}
		}
		return result.toString();
	}

	public static String forHTML(String aText) {
		final StringBuilder result = new StringBuilder();
		final StringCharacterIterator iterator = new StringCharacterIterator(
				aText);
		char character = iterator.current();
		while (character != CharacterIterator.DONE) {
			if (character == '<') {
				result.append("&lt;");
			} else if (character == '>') {
				result.append("&gt;");
			}
			// else if (character == '&') {
			// result.append("&amp;");
			// }
			else if (character == '\"') {
				result.append("&quot;");
			} else if (character == '\t') {
				addCharEntity(9, result);
			} else if (character == '!') {
				addCharEntity(33, result);
			}
			// else if (character == '#') {
			// addCharEntity(35, result);
			// }
			// else if (character == '$') {
			// addCharEntity(36, result);
			// }
			// else if (character == '%') {
			// addCharEntity(37, result);
			// }
			else if (character == '\'') {
				addCharEntity(39, result);
			} else if (character == '(') {
				addCharEntity(40, result);
			} else if (character == ')') {
				addCharEntity(41, result);
			}
			// else if (character == '*') {
			// addCharEntity(42, result);
			// }
			// else if (character == '+') {
			// addCharEntity(43, result);
			// }
			// else if (character == ',') {
			// addCharEntity(44, result);
			// }
			/*
			 * else if (character == '-') { addCharEntity(45, result); } else if
			 * (character == '.') { addCharEntity(46, result); }
			 */
			else if (character == '/') {
				addCharEntity(47, result);
			}
			// else if (character == ':') {
			// addCharEntity(58, result);
			// }
			// else if (character == ';') {
			// addCharEntity(59, result);
			// }
			// else if (character == '=') {
			// addCharEntity(61, result);
			// }
			// else if (character == '?') {
			// addCharEntity(63, result);
			// }
			/*
			 * else if (character == '@') { addCharEntity(64, result); }
			 */
			// else if (character == '[') {
			// addCharEntity(91, result);
			// }
			else if (character == '\\') {
				addCharEntity(92, result);
			}
			// else if (character == ']') {
			// addCharEntity(93, result);
			// }
			// else if (character == '^') {
			// addCharEntity(94, result);
			// }
			/*
			 * else if (character == '_') { addCharEntity(95, result); }
			 */
			else if (character == '`') {
				addCharEntity(96, result);
			} else if (character == '{') {
				addCharEntity(123, result);
			}
			// else if (character == '|') {
			// addCharEntity(124, result);
			// }
			else if (character == '}') {
				addCharEntity(125, result);
			}
			// else if (character == '~') {
			// addCharEntity(126, result);
			// }
			else {
				// the char is not a special one
				// add it to the result as is
				result.append(character);
			}
			character = iterator.next();
		}
		return result.toString();
	}

	private static void addCharEntity(Integer aIdx, StringBuilder aBuilder) {
		String padding = "";
		if (aIdx <= 9) {
			padding = "00";
		} else if (aIdx <= 99) {
			padding = "0";
		} else {
			//no prefix
		}
		String number = padding + aIdx.toString();
		aBuilder.append("&#" + number + ";");
	}
}

